<?php
$g4_path = "../"; 
require_once ($g4_path.'common.php');

$title = "강의 신청 처리";
$menu = 2;
require_once ('./admin.head.php');
require_once ('./admin.lib.php');

$tpl = new Template_;
$tpl->template_dir = './tpl';
$tpl->compile_dir = './tpl/compile';

$_sqlWhereTemp = $_sqlFrom = $_sqlSelect = $_sqlWhere = $type = $_type = $keyword = NULL;
$status = 0; 
$_tplSetting = $data = array();
$statusArray = array('1'=>'입금대기','2'=>'입금완료','3'=>'신청취소');
$statusKeyArray = array('totalnum', 'readynum', 'completenum', 'canclenum');
$searchOptionArray = array(NULL=>'검색조건 선택', 'student'=>'학생 이름', 'teacher'=>'선생님 이름', 'tel2'=>'핸드폰');

if(isset($_GET['type']) && $_GET['type'] && isset($_GET['keyword'])) { //검색 조건 설정부분
    $type = $_GET['type'];
    $keyword = $_GET['keyword'];

    if($type == 'student') {
        $_sqlSelect = ", c.student, m.mb_no"; 
        $_sqlFrom = ", g4_member m "; 
        $_sqlWhere = "WHERE m.mb_name = '$keyword' AND c.student = m.mb_no"; 
    } else if($type == "teacher") {
        $_sqlSelect = ", d.teacher, m.mb_no"; 
        $_sqlFrom = ", g4_member m, course d "; 
        $_sqlWhere = "WHERE m.mb_name = '$keyword' AND c.course = d.no AND d.teacher = m.mb_no"; 
    }
}

if(isset($_GET['status'])) { 
    $status = $_GET['status']; 
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'status', $_GET['status']);
}

$nowPage = isset($_GET['page']) ? $_GET['page'] : 1;  // 페이징 세팅부분
$perPage = 10; 
$_sqlLimitPage = ($nowPage-1)*$perPage; 
$_sqlOrder = " ORDER BY no DESC"; 
$_sqlLimit = " LIMIT $_sqlLimitPage, $perPage"; 

$sql = 'SELECT c.* '.$_sqlSelect.' FROM course_order c'.$_sqlFrom.$_sqlWhere.$_sqlOrder.$_sqlLimit;
$_res = sql_query($sql); 

require_once ('./admin.head.php');
while($_ret = sql_fetch_array($_res)) {
    $_ret['selectbox'] = _makeOptionList($statusArray, $_ret['status']);
    $_ret['memo']=htmlspecialchars(stripslashes($_ret['memo'])); 
   
    // 학생 코드로 학생 이름 가져오고.... 
    $_temp = sql_fetch("SELECT mb_name as student_name FROM g4_member WHERE mb_no = '".$_ret['student']."' LIMIT 1");
    $_ret['student_name'] = $_temp['student_name']; 
    
    // 강의 코드로 강의 이름과 담당 선생님 이름을 가져옵세
    $_temp = sql_fetch("SELECT c.name as course_name, c.teacher, m.mb_name as teacher_name FROM course c, g4_member m WHERE m.mb_no = c.teacher AND c.no = '".$_ret['course']."' LIMIT 1");
    $_ret['teacher_name'] = $_temp['teacher_name'];
    $_ret['course_name'] = $_temp['course_name'];
    $data[] = $_ret; 
}

$_sqlWhere = NULL;
$query = 'SELECT count(no) as cnt FROM course_order ';
$_tagName = "status"; 

if($type) {
    switch($type) {
        case "teacher":
            $_teachercode = isset($data[0]['teacher']) ? $data[0]['teacher'] : "null"; 
            $query = "SELECT count(c.no) as cnt FROM course_order c INNER JOIN course d ON c.course = d.no AND d.teacher = '$_teachercode'" ;
            $_sqlWhere = " ";
            $_tagName = "c.status"; 
            break;
        case "student":
            $_studentcode = isset($data[0]['student']) ? $data[0]['student'] : "null"; 
            $_sqlWhere = _makeSqlWhere(NULL, 'student', $_studentcode);
            break;
    }
}

$_temp = sizeof($statusKeyArray);
for($i=0;$i<$_temp;$i++) {
    $_sqlWhereTemp = _makeSqlWhere($_sqlWhere, $_tagName, $i); 
    $_res = sql_fetch($query.$_sqlWhereTemp);
    $key = $statusKeyArray[$i]; 
    $_tplSetting[$key] = $_res['cnt']; 
}

$totalPage = ceil($_tplSetting[$statusKeyArray[$status]]/$perPage);
if(( ($nowPage > $totalPage) && $nowPage > 1) || ($nowPage < 1)) {
    alert("페이지 번호가 잘못되었습니다.");
}

if(!$data) { //데이터가 없으면 데이터 없음 표시
    $_tplSetting['nodata'] = TRUE; 
    $totalPage = 0;
}

$_tplSetting['paging'] = get_paging($perPage,$nowPage,$totalPage,"./courseOrders.php?page=",makelink(array('page')));
//페이징에서는 status를 GET으로 넘기다가
$_tplSetting['makelink'] = makelink(array('status','page'));
//상단 링크에는 status값이 필요없음
$_tplSetting['status'] = $status;
if($type && $keyword) {
    $_tplSetting['searchKeyword'] = $keyword; 
    $_type = $type; 
}
$_tplSetting['searchType'] = _makeOptionList($searchOptionArray, $_type);

$tpl->assign('data', $data);
$tpl->assign('set', $_tplSetting);
$tpl->define('basic',"courseOrders.html");
$tpl->print_('basic');

require_once('admin.foot.php');
